<style lang="scss">
	@import "../../utils/base.scss";
	.myPosition{
		width: 95%;
		position:relative;
		left: 3%
	}
</style>
<template>
	<div ref='base' class="quest_content">
		<stepInfo ref="stepInfo" v-model="stepTitleId" :ishidden="true" :stepTitle="(applyinfo.isPartShrink===0)?stepTitle01:stepTitle02">
			<template slot="content">
				<div v-show="stepTitleId === 0">
					 <baseInfo ref="baseInfo" v-model="applyinfo" :reqId="reqId" :draftsId="draftsId" :ifedit="ifedit" :detailData="detailData" :selectData="selectData" :update="update"></baseInfo>
			    </div>
			    <div v-if="(stepTitleId===1) && (applyinfo.isPartShrink===1)" style="padding-top: 3%">
					<resourceTable ref="resourceTable" :ifedit="true" v-model="applyinfo.resources" :handleInfo="true" :editNumber="editNumber" :ifShrink="true" :detail="detail" :isPartShrink="applyinfo.isPartShrink"></resourceTable>
				</div>
				<div v-if="(stepTitleId===2) && (applyinfo.isPartShrink===1)" style="padding-top:3%;position:relative;left: 1%;">
					<div v-for="(item,key) in applyinfo.tools" :key="key">
						<Row>
							<Col span="12" style="padding-bottom: 10px">
								<Checkbox v-model="applyinfo.tools[key].check" :disabled="ifedit">
								</Checkbox>
								【工具】{{item.toolName}}
							</Col>
						</Row>
<!-- 						<Row>
							<Col span="24" style="width: 96.666667%; padding-bottom: 20px;padding-left: 20px">
							<div style="padding-bottom: 10px">
								<resourceTable v-if="applyinfo.tools[key].needResources != null && applyinfo.tools[key].needResources.length > 0"
								 :ifedit="true" v-model="applyinfo.tools[key].needResources" :handleInfo="true" :columnsList="toolColumns" :editNumber="editNumber" :ifShrink="true" :detail="detail"></resourceTable>
							</div>
							</Col>
						</Row> -->
					</div>
				</div>
				<dataList ref="dataList" v-if="(stepTitleId===3) && (applyinfo.isPartShrink===1)" v-model="applyinfo.datas" :ifedit="true" :ifShrink="true" style="padding-top: 3%" :detail="detail" :isPartShrink="applyinfo.isPartShrink"></dataList>
				<severs ref="severs" v-if="(stepTitleId===4) && (applyinfo.isPartShrink===1)" v-model="applyinfo.severs" style="padding-top: 3%" :ifedit="ifedit" :isPartShrink="applyinfo.isPartShrink"></severs>
				<component v-if="((stepTitleId===5) && (applyinfo.isPartShrink===1)) || ((stepTitleId===1) && (applyinfo.isPartShrink===0)) " ref="file" :is="'file'" :ifedit="ifedit" :filesList="filesList" v-model="applyinfo.files"></component>
				<div v-if="(stepTitleId===6) && (applyinfo.isPartShrink===1) || ((stepTitleId===2) && (applyinfo.isPartShrink===0))">
					<baseInfo v-model="applyinfo" :reqId="reqId" :draftsId="draftsId" :ifedit="ifedit" :detailData="detailData" :selectData="selectData" :update="update" :isStep6="true"></baseInfo>
					<div class="qheader" :style="{borderBottom: '0px solid rgb(210,210,210)'}">
		                <p>资源缩容内容</p>
		            </div>
		            <div class="gcontent">
		            	<div class="myPosition">
							<resourceTable :ifedit="true" v-model="applyinfo.resources" :handleInfo="true" :editNumber="editNumber" :ifShrink="true" :detail="detail" :isPartShrink="applyinfo.isPartShrink" :isStep6="true"></resourceTable>
						</div>
					</div>
					<div class="qheader" :style="{borderBottom: '0px solid rgb(210,210,210)'}">
		                <p>工具缩容内容</p>
		            </div>
					<div class="gcontent">
						<div class="myPosition">
							<div v-for="(item,key) in applyinfo.tools" :key="key">
								<Row>
									<Col span="12" style="padding-bottom: 10px">
										<Checkbox v-if="applyinfo.isPartShrink==1" v-model="applyinfo.tools[key].check" :disabled="stepTitleId==6">
										</Checkbox>
										<Checkbox v-else :value="true" :disabled="true">
										</Checkbox>
										【工具】{{item.toolName}}
									</Col>
								</Row>
							</div>
						</div>
					</div>
					<div class="qheader" :style="{borderBottom: '0px solid rgb(210,210,210)'}">
		                <p>数据缩容内容</p>
		            </div>
		            <div class="gcontent">
		            	<div class="myPosition">
							<dataList v-model="applyinfo.datas" :ifedit="true" :ifShrink="true" :detail="detail" :isPartShrink="applyinfo.isPartShrink" :isStep6="true"></dataList>
						</div>
					</div>
					<div class="qheader" :style="{borderBottom: '0px solid rgb(210,210,210)'}">
		                <p>物理机缩容内容</p>
		            </div>
		            <div class="gcontent">
		            	<div class="myPosition">
							<severs v-model="applyinfo.severs" :ifedit="ifedit" :isPartShrink="applyinfo.isPartShrink" :isStep6="true"></severs>
						</div>
					</div>
					<div class="qheader" :style="{borderBottom: '0px solid rgb(210,210,210)'}">
		                <p>附件信息</p>
		            </div>
		            <div class="gcontent">
						<component ref="file" :is="'file'" :ifedit="ifedit" :filesList="filesList" v-model="applyinfo.files"></component>
					</div>
				</div>
			</template>
		</stepInfo>
	</div>
</template>
<script>
	import ajax from "../../ajax_util"
	import Axios from 'axios'
	import projectInfo from './settledin/projectInfo'
	import resoureceInfo from './settledin/resourceInfo'
	import toolInfo from './settledin/toolInfo'
	import dataInfo from './settledin/dataInfo'
	import account from './settledin/account'
	import file from './settledin/file'
	import notice from '../../base/notice'
	import PDFJs from '../../plugins/PDFJs.js'
	import printModel from './print/print_model'
	import stepInfo from './step_info.vue'
	import excel from "../../utils/excel"
	import baseInfo from "./shrink/baseInfo.vue"
	import resourceTable from "./shrink/resource.vue"
	import dataList from './settledin/data_model.vue'
	import severs from "./shrink/service.vue"

	export default {
		name: "apply",
		components: {
			projectInfo,
			resoureceInfo,
			toolInfo,
			dataInfo,
			account,
			file,
			notice,
			printModel,
			stepInfo,
			baseInfo,
			resourceTable,
			dataList,
			severs
		},
		data() {

			return {
				stepPage: "projectInfo",
				stepTitleId: 0,
				serviceType: "entry",
				stepTitle01: [
					{
						id: 0,
						name: "基本信息",
					},
					{
						id: 1,
						name: "附件",
					},
					{
						id: 2,
						name: "概览",
					}
				],
				stepTitle02: [
					{
						id: 0,
						name: "基本信息",
					},
					{
						id: 1,
						name: "资源缩容",
					},
					{
						id: 2,
						name: "工具缩容",
					},
					{
						id: 3,
						name: "数据缩容",
					},
					// {
					// 	id: 4,
					// 	name: "账号缩容",
					// },
					{
						id: 4,
						name: "物理机缩容",
					},
					{
						id: 5,
						name: "附件",
					},
					{
						id: 6,
						name: "概览",
					}
				],
				applyinfo:{
					reqSubject:"",
					isPartShrink:1,
					user:{},
					project:{},
					roleId:0,
					projectId:0,
					resources:[],
					tools:[],
					datas:[],
					severs:[],
					files:[]
				},
				toolColumns: [
					{
						title: "资源类型",
						align: "center",
						width: 110,
						key: "clazz",
						slot: "clazz",
					},
					{
						title: "服务名称",
						align: "center",
						width: 110,
						key: "serviceName",
						slot: "serviceName"
					},
					{
						title: "细分项名",
						align: "center",
						width: 100,
						key: "subName",
						slot: "subName"
					},
					{
						title: '测算依据',
						key: 'purpose',
						minWidth: 140,
						align: 'center',
					},
					{
						title: "可选项",
						align: "center",
						minWidth: 300,
						slot: "options"
					},
					// {
					// 	title: "已分配数量",
					// 	align: "center",
					// 	width: 80,
					// 	key: "doneQuantity",
					// 	slot: "doneQuantity"
					// },
					// {
					// 	title: "正在分配数量",
					// 	align: "center",
					// 	width: 80,
					// 	key: "doingQuantity",
					// 	slot: "doingQuantity"
					// },
					// {
					// 	title: "待分配数量",
					// 	align: "center",
					// 	width: 80,
					// 	key: "waitQuantity",
					// 	slot: "waitQuantity"
					// },
					{
						title: "缩容数量",
						align: "center",
						width: 100,
						key: "quantity",
						slot: "quantity",
					},
				],
				object:{},
				selectData:{},
				detailData:{},
				detail:false,
				editNumber:true,
				update:false,
			}
		},
		created: function() {
			// this.getUserInfo();
		},
		mounted: function() {
			this.$nextTick(() => {
				if (this.reqId == null && this.draftsId == null) {
				} else if (this.reqId != null) {
					this.getApplyInfo();
					if(!this.ifedit){
						this.$refs.baseInfo.getProject(this.applyinfo.user.id)
						this.editNumber = true;
						this.detail = false;
						this.update = true;
					}
				} else if (this.draftsId != null) {
					this.getDrafts();
				}
			})
		},
		methods: {
			getApplyInfo(){
				console.log("getApplyInfo------this.processInfo--->", JSON.stringify(this.processInfo.content));
				var data = this.processInfo;
				this.applyinfo.reqSubject = data.reqSubject;
				this.applyinfo.user = data.base.userFBean;
				this.applyinfo.user.department = data.base.userFBean.dptName;
				this.applyinfo.project = data.base.project;
				this.applyinfo.roleId = data.base.role.id;
				this.applyinfo.projectId = data.base.project.id;
				this.applyinfo.files = data.files;
				this.applyinfo.isPartShrink = data.content.isPartShrink;
				this.detail = true;
				this.editNumber = false;
				this.update = false;

				if(data.content.isPartShrink == 1){
					if(data.content.resources != undefined && data.content.resources != null){
						data.content.resources.forEach(item=>{
							item.val = item.oriVal;
							item.unit = item.oriUnit;
						})

						this.applyinfo.resources = data.content.resources;
					}

					if(data.content.servers != undefined && data.content.servers != null){
						data.content.servers.forEach(item=>{
							item.val = item.oriVal;
							item.quantity = item.oriVal;
							item.unit = item.oriUnit;
						})

						this.applyinfo.severs = data.content.servers;
					}

					if(data.content.tools != undefined && data.content.tools != null){
						data.content.tools.forEach(item=>{
							item.check = true;
						})

						this.applyinfo.tools = data.content.tools;
					}

					if(data.content.datas != undefined && data.content.datas != null){
						this.applyinfo.datas = data.content.datas;
					}

					this.detailData = this.processInfo.content;

					var json = JSON.stringify(this.processInfo.content);
					var temp = JSON.parse(json);
					temp.roleId = data.base.role.id;
					temp.projectId = data.base.project.id;
					this.selectData = temp;
				}else{
					this.$refs.baseInfo.getData(this.applyinfo.projectId,0);
				}


				console.log("kkkkkkkkkk",this.applyinfo.servers,this.selectData);
			},
			getDrafts() {
				var url = this.CONFIG.base_url_test + "/draft/content/" + this.draftsId
				var that = this;
				ajax.get({
					url: url,
					that: that,
					params: null,
					success: function(data) {
						console.log("-----getDrafts-----", data);
						var json01 = JSON.stringify(data.req.object);
						var temp01 = JSON.parse(json01);
						that.applyinfo = temp01;

						var json02 = JSON.stringify(data.req.object);
						var temp02 = JSON.parse(json02);
						that.selectData = temp02;
						that.$refs.baseInfo.getProject(temp02.user.id)
						//that.getProject(temp02.user.id);
					}
				})
			},
			setApplInfo(stepId) {
				var isSubmit = false
				if (stepId == 0) {
					isSubmit = this.$refs.baseInfo.validate();
					console.log("isSubmit1------->", isSubmit)
				} else if (stepId == 1 && this.applyinfo.isPartShrink == 1) {
					isSubmit = this.$refs.resourceTable.shrinkValidate();
					console.log("isSubmit2------->", isSubmit)
				} else if (stepId == 2 && this.applyinfo.isPartShrink == 1) {
					isSubmit = true
				} else if (stepId == 3 && this.applyinfo.isPartShrink == 1) {
					isSubmit = true
				} else if (stepId == 4 && this.applyinfo.isPartShrink == 1) {
					console.log("isSubmit4------->", isSubmit)
					isSubmit = this.$refs.severs.validate();
				} else if (stepId == 5 && this.applyinfo.isPartShrink == 1) {
					isSubmit = true
				} else if (stepId == 6 && this.applyinfo.isPartShrink == 1) {
					isSubmit = true
				}else {
					isSubmit = true
				}

				return isSubmit
			},
			validate() {
				if (!this.$refs.baseInfo.validate()) {
					this.stepTitleId = 0;
					return false;
				}

				if(this.applyinfo.isPartShrink == 1){
					var isNull = true;
					if(this.applyinfo.resources != undefined && this.applyinfo.resources != null){
						this.applyinfo.resources.some(item=>{
							if(item.check != undefined && item.check != null && item.check){
								isNull = false;
								return true;
							}
						})
					}

					if(this.applyinfo.tools != undefined && this.applyinfo.tools != null && isNull){
						this.applyinfo.tools.some(item=>{
							if(item.check != undefined && item.check != null && item.check){
								isNull = false;
								return true;
							}
						})
					}

					if(this.applyinfo.severs != undefined && this.applyinfo.severs != null && isNull){
						this.applyinfo.severs.some(item=>{
							if(item.check != undefined && item.check != null && item.check){
								isNull = false;
								return true;
							}
						})
					}

					if(this.applyinfo.datas != undefined && this.applyinfo.datas != null && isNull){
						this.applyinfo.datas.some(item=>{
							if(item.check != undefined && item.check != null && item.check){
								isNull = false;
								return true;
							}
						})
					}

					if(isNull){
						this.$Message.error("请至少选择一种资源进行缩容!")
						return false;
					}
				}else{
					var resources_null = false;
					var tools_null = false;
					var severs_null = false;
					var datas_null = false;
					if(this.applyinfo.resources == undefined || this.applyinfo.resources == null || this.applyinfo.resources.length<1){
						resources_null = true;
					}

					if(this.applyinfo.tools == undefined || this.applyinfo.tools == null || this.applyinfo.tools.length<1){
						tools_null = true;
					}


					if(this.applyinfo.severs == undefined || this.applyinfo.severs == null || this.applyinfo.severs.length<1){
						severs_null = true;
					}

					if(this.applyinfo.datas == undefined || this.applyinfo.datas == null || this.applyinfo.datas.length<1){
						datas_null = true;
					}

					if( resources_null && tools_null && severs_null && datas_null ){
						this.$Message.error("请租户项目没有资源可进行缩容!")
						return false;
					}
				}

				if ( (this.stepTitleId!=6 && this.applyinfo.isPartShrink==1) || (this.stepTitleId!=2 && this.applyinfo.isPartShrink==0) ) {
					if(this.setApplInfo(this.stepTitleId)){
						if(this.applyinfo.isPartShrink==1){
							this.$refs.stepInfo.chlickStep(6)
						}else{
							this.$refs.stepInfo.chlickStep(2)
						}

						this.$Modal.info({
							title: "温馨提示：",
							content: "请最终确认申请相关信息！"
						});
						return false;
					}else{
						return false;
					}
				}

				return true;
			},
			getInfo() {
				console.log("----------ttttttttttts--",this.applyinfo);
				this.object = {};
				this.object.userId = this.applyinfo.user.id;
				this.object.user = this.applyinfo.user;
				this.object.roleId = this.applyinfo.roleId;
				this.object.role = this.applyinfo.role;
				this.object.reqSubject = this.applyinfo.reqSubject;
				this.object.projectId = this.applyinfo.projectId;
				this.object.project = this.applyinfo.project;
				this.object.files = this.applyinfo.files;

				var datas = [];
				var resources = [];
				var servers = [];
				var tools = [];
				if(this.applyinfo.datas!=undefined && this.applyinfo.datas!=null){
					this.applyinfo.datas.forEach(item=>{
						if(item.check || this.applyinfo.isPartShrink == 0){
							//var temp = {};
							item.alloId = item.id;
							item.oriVal = item.val;
							//item.service = item.service;
							//item.serviceId = item.serviceId;
							if(item.serviceId<1){
								item.serviceId = item.id;
							}
							datas.push(item)
						}
					})
				}

				if(this.applyinfo.resources!=undefined && this.applyinfo.resources!=null){
					this.applyinfo.resources.forEach(item=>{
						if(item.check || this.applyinfo.isPartShrink == 0){
							//var temp = {};
							item.alloId = item.id;
							item.oriVal = item.val;
							//item.service = item.service;
							//item.serviceId = item.serviceId;
							//item.shrinkVal = Number(item.val) - item.quantity;
							console.log("====校验===",item.options);
							item.oriUnit = item.unit;
							item.shrinkUnit = item.unit;

							resources.push(item)
						}
					})
				}

				if(this.applyinfo.severs!=undefined && this.applyinfo.severs!=null){
					this.applyinfo.severs.forEach(item=>{
						if(item.check || this.applyinfo.isPartShrink == 0){
							//var temp = {};
							item.alloId = item.id;
							item.oriVal = item.quantity;
							//item.service = item.service;
							item.serviceId = item.severId;
							//item.shrinkVal = Number(item.quantity) - item.quantitys;
							item.oriUnit = item.unit;
							item.shrinkUnit = item.unit;
							servers.push(item)
						}
					})
				}

				if(this.applyinfo.tools!=undefined && this.applyinfo.tools!=null){
					this.applyinfo.tools.forEach(item=>{
						if(item.check || this.applyinfo.isPartShrink == 0){
							//var temp = {};
							item.alloId = item.id;
							item.oriVal = item.val;
							//item.service = item.service;
							item.serviceId = item.toolId;
							tools.push(item)

							if(item.needResources != null && item.needResources != undefined){
								item.needResources.forEach(v=>{
									if(v.check || this.applyinfo.isPartShrink == 0){
										//var json = {};
										v.alloId = v.id;
										v.oriVal = v.val;
										//v.service = item.service;
										v.serviceId = v.toolId;
										//v.shrinkVal = v.quantity;
										v.oriUnit = v.unit
										v.shrinkUnit = v.unit
										resources.push(v)
									}
								})
							}
						}
					})
				}
				this.object.datas = datas;
				this.object.resources = resources;
				this.object.servers = servers;
				this.object.tools = tools;
				console.log("----------sssssssss--",this.object);

				var publicInfo = this.utils.setPublicInfoInfo(this.applyinfo.reqSubject, 0, 0, "")
				this.object.puslicInfo = publicInfo
				this.object.isPartShrink = this.applyinfo.isPartShrink
				this.object.files = this.applyinfo.files;
				var content = {};
				content.object = this.object;
				content.files = this.applyinfo.files;
				content.base = {
                    dptName:  this.applyinfo.user.department
                }
				return content;
			},
			getHandleParams(handleInfo) {
				handleInfo.reqSubject = this.applyinfo.reqSubject
				handleInfo.startTime = 0
				handleInfo.hopeTime = 0
				handleInfo.isEdit = this.isEdit;

				handleInfo.roleId = this.applyinfo.roleId;
				handleInfo.projectId = this.applyinfo.projectId;

				console.log("aaaa-------->", this.ifedit, this.isEdit == 1)
				handleInfo.isChangeContent = 0;
				if (!this.ifedit) {
					handleInfo.isChangeContent = 1;
				}
				if (this.isEdit == 1) {
					handleInfo.content = this.getInfo()
				}
			},
			getHandleUserParams(opOptions) {
				var param = {}
				param.vari = opOptions
				param.roleId = this.applyinfo.project.tenant_id;
				param.projectId = this.applyinfo.project.id;
				return param
			}
		},
		props: {
			isEdit: {},
			value: {
				default: false
			},
			ifedit: {
				type: Boolean,
				default: false,
			},
			reqId: {
				default: null
			},
			draftsId: {
				default: null
			},
			filesList: {},
			processInfo: {},
			ishidden: {
				type: Boolean,
				default: false,
			},
		},
		computed: {
		},
		watch: {
			ifedit(Val) {
				console.log("-66666666666666--ifedit---",Val);
				// if(!Val){

				// }
				this.$refs.baseInfo.getProject(this.applyinfo.user.id)
				this.editNumber = true;
				this.detail = false;
				this.update = true;
			}
		},
	}
</script>
